package com.lianxueda.drawing.config;


import com.lianxueda.drawing.constant.Constants;


import jakarta.annotation.Resource;

import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author 29264
 */
@Service
public class TemplateMsgServiceImpl implements TemplateMsgService {

    @Resource
    private WxMpService wxMpService;

    @Override
    public String sendTemplateMsg(String openid, Map<String,String> contentMap) {
        // 创建一个模板消息对象
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(openid)
                .templateId(Constants.TEMPLATE_ID)
                .build();
        // 设置模板消息的内容
        templateMessage.addData(new WxMpTemplateData("parentName", contentMap.get("parentName"), "#FF0000"));
        templateMessage.addData(new WxMpTemplateData("courseName", contentMap.get("courseName"), "#FF0000"));
        templateMessage.addData(new WxMpTemplateData("studentName", contentMap.get("studentName"), "#FF0000"));
        templateMessage.addData(new WxMpTemplateData("schoolTime", contentMap.get("schoolTime"), "#FF0000"));
        templateMessage.addData(new WxMpTemplateData("classLocation", contentMap.get("classLocation"), "#FF0000"));

        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            return "通知消息发送成功！";
        } catch (WxErrorException e) {
            return "通知消息发送失败，发送失败原因：" + e.getMessage();
        }

    }

    @Override
    public String sendCheckMsg(String openid, Map<String,String> contentMap) {
        // 创建一个模板消息对象
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(openid)
                .templateId(Constants.CHECKING_IN)
                .build();
        // 设置模板消息的内容
        templateMessage.addData(new WxMpTemplateData("parentName", contentMap.get("parentName"), "#FF0000"));
        templateMessage.addData(new WxMpTemplateData("courseName", contentMap.get("courseName"), "#FF0000"));
        templateMessage.addData(new WxMpTemplateData("scheduleName", contentMap.get("scheduleName"), "#FF0000"));
        templateMessage.addData(new WxMpTemplateData("studentName", contentMap.get("studentName"), "#FF0000"));
        templateMessage.addData(new WxMpTemplateData("schoolTime", contentMap.get("schoolTime"), "#FF0000"));
        templateMessage.addData(new WxMpTemplateData("classLocation", contentMap.get("classLocation"), "#FF0000"));

        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            return "通知消息发送成功！";
        } catch (WxErrorException e) {
            return "通知消息发送失败，发送失败原因：" + e.getMessage();
        }

    }
}
